** Adjust for unspecified drugs on death certificates a la Ruhm.

local process_data = 0
local predict_notspecify = 1

if `process_data' == 1 {

	use ..\processed_data\drugpois_cleaned_covs.dta, clear

	** specific drugs or unspecified or categories
	quietly {
		gen unspecified = 0
		gen heroin = 0
		gen opioids = 0

		gen narcotics = 0
		gen other_narcotics = 0
		gen sedatives = 0
		gen psychotropics = 0
		gen other_specified = 0
		gen alcohol = 0

		forvalues i = 1(1)20 {
			display "You are on condition `i'"
			quietly {
				gen num = regexs(1) if regexm(cond`i',"([0-9]+)")
				gen let = regexs(1) if regexm(cond`i',"([A-Z]+)")
				destring num, replace
				replace unspecified = 1 if cond`i'=="T509"
				replace heroin = 1 if cond`i'=="T401"
				replace opioids = 1 if (let=="T" & inrange(num,402,404) )
				replace narcotics = 1 if (let=="T" & inrange(num,400,409) )
				replace other_narcotics = 1 if (let=="T" & (inrange(num,406,409) | num==400) )
				replace sedatives = 1 if (let=="T" & inrange(num,420,428) )
				replace psychotropics = 1 if (let=="T" & inrange(num,430,439) )
				replace other_specified = 1 if (let=="T" & ( inrange(num,360,389) | inrange(num,440,487) | inrange(num,490,508) | num==410 | num==419) )
				replace alcohol = 1 if (let=="T" & inrange(num,510,514) )
				
				drop num let
			}
		}
	}

	egen specified = rowtotal(narcotics sedatives psychotropics other_specified alcohol)
	gen spec1 = specified>0
	** make county-year rate of specification
	egen specify = mean(spec1), by(statefips countyfips death_year)
	drop cond1-cond20
	compress 
	save ..\processed_data\for_pred_specify.dta, replace

}

if `predict_notspecify' == 1 {
	use ..\processed_data\for_pred_specify.dta, clear

	gen og_specify = specify
	gen h_hat = .
	gen h_hat_spec = .
	** predict heroin deaths and make adjustment for fraction specified
	forvalues yy = 2004(1)2014 {
		forvalues mm = 1(1)12 {
			probit heroin specify male race_black race_other married ///
			edu_lths edu_hs edu_sco edu_co age2029 age3039 age4049 age5059 age6069 age7079 age80 ///
			ddd2-ddd8 div2-div9 if death_year==`yy' & death_month==`mm'
			predict temp1 if death_year==`yy' & death_month==`mm', pr
			replace h_hat = temp1 if death_year==`yy' & death_month==`mm'
			replace specify=1
			predict temp2 if death_year==`yy' & death_month==`mm', pr
			replace h_hat_spec = temp2 if death_year==`yy' & death_month==`mm'
			replace specify = og_specify		
			drop temp1 temp2
		}
	}
	
	** predict opioid deaths and make adjustment for fraction specified
	gen o_hat = .
	gen o_hat_spec = .
	forvalues yy = 2004(1)2014 {
		forvalues mm = 1(1)12 {
			probit opioid specify male race_black race_other married ///
			edu_lths edu_hs edu_sco edu_co age2029 age3039 age4049 age5059 age6069 age7079 age80 ///
			ddd2-ddd8 div2-div9 if death_year==`yy' & death_month==`mm'
			predict temp1 if death_year==`yy' & death_month==`mm', pr
			replace o_hat = temp1 if death_year==`yy' & death_month==`mm'
			replace specify=1
			predict temp2 if death_year==`yy' & death_month==`mm', pr
			replace o_hat_spec = temp2 if death_year==`yy' & death_month==`mm'
			replace specify = og_specify		
			drop temp1 temp2
		}
	}
	
	** predict heroin or opioid deaths and make adjustment for fraction specified
	gen horo_hat = .
	gen horo_hat_spec = .
	gen horo = heroin | opioid
	forvalues yy = 2004(1)2014 {
		forvalues mm = 1(1)12 {
			probit horo specify male race_black race_other married ///
			edu_lths edu_hs edu_sco edu_co age2029 age3039 age4049 age5059 age6069 age7079 age80 ///
			ddd2-ddd8 div2-div9 if death_year==`yy' & death_month==`mm'
			predict temp1 if death_year==`yy' & death_month==`mm', pr
			replace horo_hat = temp1 if death_year==`yy' & death_month==`mm'
			replace specify=1
			predict temp2 if death_year==`yy' & death_month==`mm', pr
			replace horo_hat_spec = temp2 if death_year==`yy' & death_month==`mm'
			replace specify = og_specify		
			drop temp1 temp2
		}
	}
	
	** collapse up to the state level to make counts of the numbers of each type
	** of death you are interested in.
	collapse (sum) heroin opioid horo h_hat h_hat_spec o_hat o_hat_spec horo_hat ///
	horo_hat_spec, by(statefips death_month death_year)
	rename statefips fips
	rename death_month month
	rename death_year year
	compress
	sort fips year month
	save ..\processed_data\pred_deaths.dta, replace
	
	
}
